package com.cafuc.entity;

import java.io.*;
import java.util.Date;

@MyAnnotation(type = 1, value = "sun",
        imageType=ImageType.SMALL_IMAGE, cities={"cd", "cq"})
public class User implements Serializable, Cloneable {
    private int id;
    @MyAnnotation(type = 2, value = "sun2",
            imageType=ImageType.SMALL_IMAGE, cities={"cd2", "cq2"})
    private String userName;
    private String password;
    private String name;
    private int age;
    private Date createDate;
    private Card card;

    public User() {
    }

    public User(int id, String userName, int age) {
        this.id = id;
        this.userName = userName;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPassword() {
        return password;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

    @MyAnnotation(type = 3, value = "sun3",
            imageType=ImageType.SMALL_IMAGE, cities={"cd3", "cq3"})
    public void call() {
        System.out.println("==");
    }

    public static void display() {
        System.out.println("asdfghjkl");
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public User deepClone() throws IOException, ClassNotFoundException {
        // 使用 ObjectOutputStream 将内存的当前对象序列化到 ByteArrayOutputStream
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(this);

        // 使用 ObjectInputStream 将字节数组中的的内容反序列化为对象输出
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        return (User) ois.readObject();
    }

    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException, IOException, ClassNotFoundException {
//        JavaBase javaBase = new JavaBase() {
//            @Override
//            public void call() {
//                System.out.println("hello");
//            }
//        };
//        javaBase.call();
//
//        MyAnnotation myAnnotation1 = User.class.getAnnotation(MyAnnotation.class);
//        System.out.println(myAnnotation1.type() + "==" +
//                myAnnotation1.value() + "--" + Arrays.asList(myAnnotation1.cities()));
//        MyAnnotation myAnnotation2 = User.class.getDeclaredField("name").getAnnotation(MyAnnotation.class);
//        System.out.println(myAnnotation2.type() + "==" +
//                myAnnotation2.value() + "--" + Arrays.asList(myAnnotation2.cities()));
//        MyAnnotation myAnnotation3 = User.class.getDeclaredMethod("call", null).getAnnotation(MyAnnotation.class);
//        System.out.println(myAnnotation3.type() + "==" +
//                myAnnotation3.value() + "--" + Arrays.asList(myAnnotation3.cities()));

        Card card = new Card();
        card.setId(1);
        card.setNo("8786543213456787907");
        User source = new User();
        source.setId(1);
        source.setUserName("sun");
        source.setAge(18);
        source.setCard(card);

        // User target = (User) source.clone();
        User target = (User) source.deepClone();
        source.setUserName("sun");
        source.setAge(19);
        source.getCard().setNo("20220200202222020");
        System.out.println(source.getUserName() + "-" +
                source.getAge() + "-" + source.getCard().getNo());
        System.out.println(target.getUserName() + "-" +
                target.getAge() + "-" + target.getCard().getNo());
    }
}